package com.xin.ruanjian.omsystem.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.xin.ruanjian.omsystem.R;
import com.xin.ruanjian.omsystem.app.MyApplication;
import com.xin.ruanjian.omsystem.bases.BaseActivity;
import com.xin.ruanjian.omsystem.bases.Login;
import com.xin.ruanjian.omsystem.bean.IndexBean.MsgBean.ShopBean;
import com.xin.ruanjian.omsystem.model.ShopCarModel;
import com.xin.ruanjian.omsystem.utils.GlideUtils;
import com.xin.ruanjian.omsystem.utils.PhoneFormatCheckUtils;
import com.xin.ruanjian.omsystem.utils.ShopCarUtils;
import com.xin.ruanjian.omsystem.view.TimeSelectDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 在线订座页面
 */

public class OnlineReservationsActivity extends BaseActivity {

    @BindView(R.id.tv_centerTitle)
    TextView tvCenterTitle;
    @BindView(R.id.iv_rightPic)
    ImageView ivRightPic;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.iv_shop_head)
    ImageView ivShopHead;
    @BindView(R.id.tv_shop_name)
    TextView tvShopName;
    @BindView(R.id.tv_sale_average)
    TextView tvSaleAverage;
    @BindView(R.id.tv_kouwei)
    TextView tvKouwei;
    @BindView(R.id.tv_huanjing)
    TextView tvHuanjing;
    @BindView(R.id.tv_fuwu)
    TextView tvFuwu;
    @BindView(R.id.ll_xingxing)
    LinearLayout llXingxing;
    @BindView(R.id.tv_yixuan_caipin)
    TextView tvYixuanCaipin;
    @BindView(R.id.ll_yi_xuan_cai_pin)
    LinearLayout llYiXuanCaiPin;
    @BindView(R.id.tv_one)
    TextView tvOne;
    @BindView(R.id.tv_two)
    TextView tvTwo;
    @BindView(R.id.tv_three)
    TextView tvThree;
    @BindView(R.id.tv_other)
    TextView tvOther;
    @BindView(R.id.et_person_num)
    EditText etPersonNum;
    @BindView(R.id.tv_time)
    public TextView tvTime;
    @BindView(R.id.textView2)
    TextView textView2;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.tv_nvshi)
    TextView tvNvshi;
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.tv_xiansheng)
    TextView tvXiansheng;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_yaoqiu_zuowei)
    EditText etYaoqiuZuowei;
    @BindView(R.id.et_yaoqiu_cai_pin)
    EditText etYaoqiuCaiPin;
    @BindView(R.id.tb_other)
    ToggleButton tbOther;
    @BindView(R.id.et_other_name)
    EditText etOtherName;
    @BindView(R.id.tv_other_nvshi)
    TextView tvOtherNvshi;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.tv_other_xiansheng)
    TextView tvOtherXiansheng;
    @BindView(R.id.et_other_phone)
    EditText etOtherPhone;
    @BindView(R.id.ll_other)
    LinearLayout llOther;
    @BindView(R.id.bt_tijiao)
    Button btTijiao;

    private Login login = MyApplication.getLogin();
    private ShopBean shopBean;
    //1代表先生，0代表女士
    private int num = 0, sex = 1, other_sex = 1;
    //订餐时间
    private String time, openTime, closeTime;
    @Override
    protected void initContentView(Bundle bundle) {
        setContentView(R.layout.activity_online_reservations);
    }

    @Override
    protected void initData() {
        shopBean = (ShopBean) getIntent().getSerializableExtra("shop_info");
    }

    @Override
    protected void initView() {
        etPersonNum.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    num = 0;
                    tvOne.setTextColor(getResources().getColor(R.color.text_gray));
                    tvTwo.setTextColor(getResources().getColor(R.color.text_gray));
                    tvThree.setTextColor(getResources().getColor(R.color.text_gray));
                }
                return false;
            }
        });
        tbOther.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    llOther.setVisibility(View.VISIBLE);
                }else {
                    llOther.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    protected void initTitle() {
        showDefaultBackIcon();
        setTvCenterTitle("在线订座");
    }

    @Override
    protected void main() {
        tvShopName.setText(shopBean.getShopname());
        tvSaleAverage.setText(shopBean.getShopsale()+"元/人 | 月销" + shopBean.getAverage() + "笔");
        GlideUtils.glideLoader(context, shopBean.getShophead(), ivShopHead, GlideUtils.Shape.SQUARE);
        for (int i = 0;i < 5;i++) {
            ImageView imageView = new ImageView(this);
            if (i < shopBean.getShopstars()) {
                imageView.setImageResource(R.mipmap.home_icon_1);
            }else {
                imageView.setImageResource(R.mipmap.home_icon_1_gray);
            }
            llXingxing.addView(imageView);
        }
        String selected = "";
        for (ShopCarModel model : ShopCarUtils.getShopCarData(shopBean.getShopid(), login.getUserId())) {
            selected += model.getdName() + "  ";
        }
        tvYixuanCaipin.setText(selected);
        String[] split = shopBean.getOpentime().split("-");
        if (split.length > 1) {
            openTime = split[0];
            closeTime = split[1];
        }
    }

    public void goToNextStep() {
        if (num == 0 && TextUtils.isEmpty(etPersonNum.getText())) {
            Toast("请选择就餐人数");
        }else if (TextUtils.isEmpty(tvTime.getText())){
            Toast("请选择用餐时间");
        }else if (TextUtils.isEmpty(etName.getText())) {
            Toast("请输入您的姓名");
        }else if (TextUtils.isEmpty(etPhone.getText())) {
            Toast("请输入您的手机号码");
        }else if (!PhoneFormatCheckUtils.isPhoneLegal(etPhone.getText().toString())) {
            Toast("请输入正确的号码格式");
        } else {
            startActivityForResult(new Intent(context, TsConfirmOrderActivity.class)
                    .putExtra("shop_info", shopBean)
            .putExtra("table_num", (num == 0 ? etPersonNum.getText().toString() : num+"") + "人")
            .putExtra("table_time", tvTime.getText())
            .putExtra("user_name", etName.getText().toString())
            .putExtra("user_phone", etPhone.getText().toString())
            .putExtra("user_sex", sex == 0 ? "女士" : "先生")
            .putExtra("seat_request", etYaoqiuZuowei.getText().toString())
            .putExtra("dish_request", etYaoqiuCaiPin.getText().toString()), 1100);
        }
    }

    @OnClick({R.id.tv_one, R.id.tv_two, R.id.tv_three, R.id.rl_time, R.id.tb_other, R.id.bt_tijiao,
    R.id.tv_xiansheng, R.id.tv_nvshi, R.id.tv_other_xiansheng, R.id.tv_other_nvshi})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_one:
                num = 1;
                tvOne.setTextColor(getResources().getColor(R.color.text_black));
                tvTwo.setTextColor(getResources().getColor(R.color.text_gray));
                tvThree.setTextColor(getResources().getColor(R.color.text_gray));
                etPersonNum.setText("");
                etPersonNum.clearFocus();
                break;
            case R.id.tv_two:
                num = 2;
                tvOne.setTextColor(getResources().getColor(R.color.text_gray));
                tvTwo.setTextColor(getResources().getColor(R.color.text_black));
                tvThree.setTextColor(getResources().getColor(R.color.text_gray));
                etPersonNum.setText("");
                etPersonNum.clearFocus();
                break;
            case R.id.tv_three:
                num = 3;
                tvOne.setTextColor(getResources().getColor(R.color.text_gray));
                tvTwo.setTextColor(getResources().getColor(R.color.text_gray));
                tvThree.setTextColor(getResources().getColor(R.color.text_black));
                etPersonNum.setText("");
                etPersonNum.clearFocus();
                break;
            case R.id.rl_time:
                TimeSelectDialog dialog = TimeSelectDialog.newInstance(openTime, closeTime);
                dialog.show(getSupportFragmentManager(), "time");
                break;
            case R.id.tb_other:
                break;
            case R.id.bt_tijiao:
                goToNextStep();
                break;
            case R.id.tv_xiansheng:
                sex = 1;
                tvXiansheng.setTextColor(getResources().getColor(R.color.color_orange));
                tvNvshi.setTextColor(getResources().getColor(R.color.text_gray));
                break;
            case R.id.tv_nvshi:
                sex = 0;
                tvXiansheng.setTextColor(getResources().getColor(R.color.text_gray));
                tvNvshi.setTextColor(getResources().getColor(R.color.color_orange));
                break;
            case R.id.tv_other_xiansheng:
                other_sex = 1;
                tvOtherXiansheng.setTextColor(getResources().getColor(R.color.color_orange));
                tvOtherNvshi.setTextColor(getResources().getColor(R.color.text_gray));
                break;
            case R.id.tv_other_nvshi:
                other_sex = 0;
                tvOtherXiansheng.setTextColor(getResources().getColor(R.color.text_gray));
                tvOtherNvshi.setTextColor(getResources().getColor(R.color.color_orange));
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1100 && resultCode == 1101) {
            setResult(1001);
            finish();
        }
    }
}
